<?php

class BidController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function sellerbidAction()
    {
        $pb_bid_model = new Application_Model_ProductBuyBid();
        $pb_bid_mapper = new Application_Model_ProductBuyBidMapper();
        $pb_offer_model = new Application_Model_ProductBuyOffer();
        $pb_offer_mapper = new Application_Model_ProductBuyOfferMapper();
        
        $sessLogin = new Zend_Session_Namespace('Login');
        Zend_Date::setOptions(array('format_type' => 'php'));
        $date = new Zend_Date();
        
        $_price = $this->getRequest()->getPost('price');
        $_pbid = $this->getRequest()->getPost('pbid');
        $_offerCounter = $this->getRequest()->getPost('offerCounter');

        $pb_bid_model->setProduct_buy_id($_pbid);
        $pb_bid_model->setMember_id($sessLogin->mbmId);
        $pb_bid_model->setPrice($_price);
        $pb_bid_model->setOffer_type("O");
        $pb_bid_model->setStatus("W");
        $pb_bid_model->setChecked("N");
        $pb_bid_model->setCreation_time($date->toString('Y-m-d H:i:s'));
        
        $pb_bid_mapper->insertAll($pb_bid_model);
        
        $pbBidEntry = $pb_bid_mapper->findByMemberIdPbIdCreationTime($sessLogin->mbmId, $_pbid, $date->toString('Y-m-d H:i:s'));
        
        for($i = 1; $i < $_offerCounter + 1; $i++){
        	if($this->getRequest()->getPost('offer' . $i) != null){
        		$pb_offer_model->setProduct_buy_bid_id($pbBidEntry[0]->product_buy_bid_id);
        		$pb_offer_model->setOffer($this->getRequest()->getPost('offer' . $i));
        		$pb_offer_model->setOffer_type("O");
        		$pb_offer_model->setStatus("W");
        		$pb_offer_model->setCreation_time($date->toString('Y-m-d H:i:s'));
        		
        		$pb_offer_mapper->insertAll($pb_offer_model);
        	}
        }
        
        
        $this->view->assign('action' , '/product/sellerbid/pid/' . $_pbid);
        $this->view->assign('title' , '訊息提示');
        $this->view->assign('result' , '已投標!現在將轉到拍賣品頁面。');
        $this->view->assign('redirect' , '如果您的瀏覽器沒有自動跳轉，請點擊這裡');
    }

    public function buyeracceptofferAction()
    {
        $mbm_mapper = new Application_Model_MemberMapper();
                        $pb_mapper = new Application_Model_ProductBuyMapper();
                        $pb_bid_mapper = new Application_Model_ProductBuyBidMapper();
                                        
                                        $sessLogin = new Zend_Session_Namespace('Login');
										Zend_Date::setOptions(array('format_type' => 'php'));
                                        $date = new Zend_Date();
                                        $_pbid = $this->getRequest()->getPost('pbid');
                                        $_pbBid = $this->getRequest()->getPost('pbbid');
                                        
                                        $pbEntry = $pb_mapper->findByMemberIdPbId($sessLogin->mbmId, $_pbid);
                                        $pbBidEntry = $pb_bid_mapper->findByProduct_buy_bid_id($_pbBid);
                                
                                        if($pbEntry != NULL){
                                        	$pbEntry[0]->setStatus('F');
                                        	$pbEntry[0]->setWinner($pbBidEntry[0]->member_id);
                                        	$pb_mapper->updateAll($pbEntry[0]);
                                        	
                                        	$pbBidEntry[0]->setChecked("Y");
                                        	$pb_bid_mapper->updateAll($pbBidEntry[0]);
                                        }
                                        
                                        /*** update credit ***/
                                        $default_deal_credit = 10;
                                        
                                        $mbmEntry = $mbm_mapper->findByMemberId($sessLogin->mbmId);
                                        $exsit_deal_credit = $mbmEntry[0]->deal_credit;
                                        $total_deal = $mbmEntry[0]->total_deal;
                                        $mbmEntry[0]->setDeal_credit($exsit_deal_credit + $default_deal_credit);
                                        $mbmEntry[0]->setTotal_deal($total_deal + 1);
                                        $mbm_mapper->updateAll($mbmEntry[0]);
                                        
                                        $mbmId = $pbBidEntry[0]->member_id;
                                        $mbmEntry = $mbm_mapper->findByMemberId($mbmId);
                                        $exsit_deal_credit = $mbmEntry[0]->deal_credit;
                                        $total_deal = $mbmEntry[0]->total_deal;
                                        $mbmEntry[0]->setDeal_credit($exsit_deal_credit + $default_deal_credit);
                                        $mbmEntry[0]->setTotal_deal($total_deal + 1);
                                        $mbm_mapper->updateAll($mbmEntry[0]);
                                        
                                        $this->_redirect('/bid/rating/pbbid/' . $_pbBid);
    }

    public function counterofferAction()
    {
        $pb_mapper = new Application_Model_ProductBuyMapper();
        $pb_bid_model = new Application_Model_ProductBuyBid();
        $pb_bid_mapper = new Application_Model_ProductBuyBidMapper();
        $pb_offer_model = new Application_Model_ProductBuyOffer();
        $pb_offer_mapper = new Application_Model_ProductBuyOfferMapper();
        
        $sessLogin = new Zend_Session_Namespace('Login');
        Zend_Date::setOptions(array('format_type' => 'php'));
        $date = new Zend_Date();
        $offer_type = "";
        
        $_pbid = $this->getRequest()->getPost('pbid');
        $_pbBid = $this->getRequest()->getPost('pbbid');
        $_bidPrice = $this->getRequest()->getPost('radioBidPrice');
        
        $pbEntry = $pb_mapper->findByMemberIdPbId($sessLogin->mbmId, $_pbid);
        if($pbEntry == NULL){
        	$offer_type = "O"; 
        }
        else{
        	$offer_type = "CO";
        }
        
        /*** update counter offer price ***/
        $pbBidEntry = $pb_bid_mapper->findByProduct_buy_bid_id($_pbBid);
        if($_bidPrice == "accept"){
        	$pb_bid_model->setStatus("A");
        	$pb_bid_model->setPrice($pbBidEntry[0]->price);
        }
        else if($_bidPrice == "reject"){
        	$_cOfferPrice = $this->getRequest()->getPost('cOfferPrice');
			$pb_bid_model->setPrice($_cOfferPrice);
			$pb_bid_model->setStatus("R");
        }
        
        $pb_bid_model->setProduct_buy_id($_pbid);
        $pb_bid_model->setMember_id($pbBidEntry[0]->member_id);
        $pb_bid_model->setOffer_type($offer_type);
        $pb_bid_model->setChecked("N");
        $pb_bid_model->setCreation_time($date->toString('Y-m-d H:i:s'));
        
        $pb_bid_mapper->insertAll($pb_bid_model);
        
        $pbBidEntry[0]->setChecked("Y");
        $pb_bid_mapper->updateAll($pbBidEntry[0]);
        
        /*** update counter offer condition ***/
        $pbOfferEntries = $pb_offer_mapper->findByProduct_buy_bid_id($_pbBid);
        $newPbBidEntry = $pb_bid_mapper->findByMemberIdPbIdCreationTime($pbBidEntry[0]->member_id, $_pbid, $date->toString('Y-m-d H:i:s'));
        for($i = 0; $i < sizeof($pbOfferEntries); $i++){
        	$_bidOffer = $this->getRequest()->getPost('radioBid' . $pbOfferEntries[$i]->product_buy_offer_id);
        	
        	if($_bidOffer == "accept"){
        		$pb_offer_model->setStatus("A");
        		$pb_offer_model->setOffer($pbOfferEntries[$i]->offer);
        	}
        	else if($_bidOffer == "reject"){
        		$_cOffer = $this->getRequest()->getPost('cOffer' . $pbOfferEntries[$i]->product_buy_offer_id);
        		$pb_offer_model->setStatus("R");
        		$pb_offer_model->setOffer($_cOffer);
        	}
        	
        	$pb_offer_model->setProduct_buy_bid_id($newPbBidEntry[0]->product_buy_bid_id);
        	$pb_offer_model->setOffer_type($offer_type);
        	$pb_offer_model->setCreation_time($date->toString('Y-m-d H:i:s'));
        	
        	$pb_offer_mapper->insertAll($pb_offer_model);
        }
        
        $this->view->assign('action' , '/product/sellerbid/pid/' . $_pbid);
        $this->view->assign('title' , '訊息提示');
        $this->view->assign('result' , '已還價!現在將轉到拍賣品頁面。');
         $this->view->assign('redirect' , '如果您的瀏覽器沒有自動跳轉，請點擊這裡');
    }

    public function sellercancelofferAction()
    {
        $pb_bid_mapper = new Application_Model_ProductBuyBidMapper();
                        $pb_offer_mapper = new Application_Model_ProductBuyOfferMapper();
                            	
                        $_pbbid = $this->getRequest()->getPost('pbbid');
                        
                        $pb_bid_mapper->deleteProductBuyBid($_pbbid);
                        $pb_offer_mapper->deleteProductBuyOfferByPbBid($_pbbid);
    }

    public function ratingAction()
    {
            	$request = $this->getRequest();
                $_pbBid = $request->getParam('pbbid');
                
                $this->view->assign('action', '/bid/rate/pbbid/' . $_pbBid);
                $this->view->assign('title' , '評分');
                $this->view->assign('submit' , '提交');
    }

    public function rateAction()
    {
    	$mbm_mapper = new Application_Model_MemberMapper();
    	$pb_bid_mapper = new Application_Model_ProductBuyBidMapper();
    	
    	$request = $this->getRequest();
    	$_pbBid = $request->getParam('pbbid');
        $_rating = $this->getRequest()->getPost('ratingGroup');
        
        $pbBidEntry = $pb_bid_mapper->findByProduct_buy_bid_id($_pbBid);
        $mbmId = $pbBidEntry[0]->member_id;
        
        $mbmEntry = $mbm_mapper->findByMemberId($mbmId);
        $exist_rating_credit = $mbmEntry[0]->rating_credit;
        $total_deal = $mbmEntry[0]->total_deal;

        if($exist_rating_credit == 0){
        	$average_rating = $_rating / $total_deal;
        	$mbmEntry[0]->setRating_credit($average_rating);
        }
        else{
        	$average_rating = ($exist_rating_credit + $_rating) / $total_deal;
        	$mbmEntry[0]->setRating_credit($average_rating);
        }
        $mbm_mapper->updateAll($mbmEntry[0]);
        
        $this->view->assign('action' , '/member/myproduct');
        $this->view->assign('title' , '訊息提示');
        $this->view->assign('result' , '已評分!現在將轉到拍賣品頁面。');
        $this->view->assign('redirect' , '如果您的瀏覽器沒有自動跳轉，請點擊這裡');
    }


}













